// pages/check_list/check_list.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
const {
  requestDataparams
} = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checktasknodeid: '',
    isLeader: false,
    nodeStatus: '',
    leaderConfirmed: false,
    hcchooseList: ["121"],
    disable: false,
    lists: [],
    code: '',
    checkTaskContents: [],
    foreignId: '',
    deviceClassificationId: '',
    checkTaskId: '',
    checkStandardId: '',
    platform: '',
    selected: 1,
    chooseList: [],
    listselected: '1',
    list: [{
        name: '指示灯',
        id: 1,
        sublist: [{
            name: '指示灯正常',
            id: 222,
            status: false,
          },
          {
            name: '目视检查',
            id: 222,
            status: false,
          }
        ]
      },
      {
        name: '柜体及外观',
        id: 1,
        sublist: [{
            name: '无异味',
            id: 222,
            status: false,
          },
          {
            name: '无打火烧痕',
            id: 222,
            status: false,
          }
        ]
      }

    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.data.foreignId = options.id;
    }
    if (options.checkTaskId) {
      this.data.checkTaskId = options.checkTaskId;
    }
    if (options.deviceClassificationId) {
      this.data.deviceClassificationId = options.deviceClassificationId;
    }
    if (options.checkname) {
      this.data.checkname = options.checkname;
    }
    if (options.code) {
      this.data.code = options.code;
    }
    if (options.scancode) {
      this.data.scancode = options.scancode;
    }
    if (options.leaderConfirmed) {
      this.data.leaderConfirmed = options.leaderConfirmed;
    }
    if (options.nodeStatus) {
      this.data.nodeStatus = options.nodeStatus;
    }
    if (options.isLeader) {
      this.data.isLeader = options.isLeader;
    }
    if (options.checktasknodeid) {
      this.data.checktasknodeid = options.checktasknodeid;
    }

    this.setData(this.data);
    this.loadData();
  },
  // 选择
  selectsataus: function (e) {

    var that = this;
    // if (!that.data.disable) {
    //   wx.showToast({
    //     title: '请先扫码确认是否是任务设备',
    //     icon: 'none',
    //     duration: 3000
    //   })
    //   return
    // }



    if (!that.data.isLeader) {
      if (that.data.leaderConfirmed == 'true') {
        wx.showToast({
          title: '主管已确认过',
          icon: 'none'
        })
        return
      }

    }
    if (!that.data.scancode || that.data.scancode == "undefined") {
      wx.showToast({
        title: '请先扫码',
        icon: 'none',
        duration: 2000
      })
      return
    }

    if (that.data.code != that.data.scancode) {
      wx.showToast({
        title: '不是当前设备不能提交',
        icon: 'none',
        duration: 2000
      })
      return
    }
    var id = e.currentTarget.dataset.cid;

    // var item = this.data.vehicleTypeItems[index]
    // item.isSelected = !item.isSelected;
    // var chooseList = [];
    var choosecheckStandardId = '';
    var n = 0;

    for (var i in that.data.lists) {
      var lista = that.data.lists[i];
      // 
      for (var j in lista.checkContentVOs) {
        // 

        var pro = lista.checkContentVOs;

        if (pro[j].id == id) {

          if (pro[j].contentStatus == 1) {
            pro[j].contentStatus = 2;


          } else {
            pro[j].contentStatus = 1;
          }

          let index = that.data.chooseList.findIndex((obj, i, objs) => {
            return obj.checkStandardId == lista.id && obj.checkContentId == pro[j].id
          });

          if (index != -1) {
            that.data.chooseList[index].contentStatus = pro[j].contentStatus;
          } else {
            that.data.chooseList.push({
              'checkStandardId': lista.id,
              'checkContentId': pro[j].id,
              'contentStatus': pro[j].contentStatus
            });
          }
        }
      }
    }
    // that.data.checkStandardId = choosecheckStandardId;
    // that.data.checkTaskContents = chooseList;
    that.setData(that.data);
    // 

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  selected: function (e) {
    var t = e.currentTarget.dataset.t;
    this.setData({
      selected: t
    })
  },
  listselected1: function (e) {
    var t = e.currentTarget.dataset.t;
    var index = e.currentTarget.dataset.index;
    var item = this.data.lists[index]
    item.open = !item.open;

    this.setData({
      listselected: t,
      navNumber: t
    })
    this.setData(this.data);
  },
  listselected: function (e) {
    var t = e.currentTarget.dataset.t;
    var index = e.currentTarget.dataset.index;
    var item = this.data.lists[index]
    item.open = !item.open;
    this.setData({
      listselected: 0,
      navNumber: 0
    });
    this.setData(this.data);
  },
  scan: function () {
    var that = this;
    wx.scanCode({
      success(res) {


        if (res.result == that.data.code) {
          that.setData({
            disable: true
          })
          wx.showToast({
            title: '扫码成功',
            icon: 'success'

          })
        } else {
          wx.showToast({
            title: '任务设备和当前设备不匹配，请选择正确设备',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail() {
        wx.showToast({
          title: '扫描失败',
          icon: 'fail',
          duration: 3000
        })
      }
    })
  },
  hqbtn() {
    var that = this;
    if (!that.data.disable) {
      wx.showToast({
        title: '请先扫码确认是否是任务设备',
        icon: 'none',
        duration: 3000
      })
      return
    }
    wx.showToast({
      title: '已缓存',
      icon: 'fail',
      duration: 2000
    });


  },
  submitPra: function () {
    var that = this;
    // if (!that.data.chooseList.length > 0) {
    //   wx.showToast({
    //     title: '您尚未修改，请修改后保存',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return
    // }
    if (that.data.isLeader != 'true') {

      if (that.data.nodeStatus == 2 || that.data.nodeStatus == 3) {
        wx.showToast({
          title: '您已经巡检过此设备',
          icon: 'none'
        });

        return
      }
    }
    if (that.data.code != that.data.scancode) {
      wx.showToast({
        title: '不是当前设备不能提交',
        icon: 'none',
        duration: 2000
      })
      return
    }
    var arra = that.data.chooseList.reverse();
    var arrb = Array.from(new Set(arra));
    var data = {
      'checkTaskId': that.data.checkTaskId,
      'foreignId': that.data.foreignId,
      'checkTaskContents': arrb,
      foreignType: 2
    }
    // wx.request({
    //   url: applet_park_api_url + '/api/checkContents',
    //   method: 'PUT',
    //   header: {
    //     contentType: "application/json",
    //   },
    //   data: JSON.stringify(data),
    //   success: function(res) {
    //     )
    //     if (res.data.code == 0) {
    //       wx.showToast({
    //         title: '保存成功',
    //         icon: 'success',
    //         duration: 2000
    //       })
    //       var pages = getCurrentPages();
    //       var prevPage = pages[pages.length - 2]; //上一个页面
    //       prevPage.loadData();

    //       prevPage.setData(prevPage.data);
    //       wx.navigateBack();
    //       // wx.redirectTo({
    //       //   url: '../patrol_record/patrol_record',
    //       // })

    //     } else {
    //       wx.showToast({
    //         title: res.data.message,
    //         icon: 'none'
    //       });
    //     }
    //   },
    //   fail: function(res) {
    //     wx.showToast({
    //       title: '网络异常',
    //     });
    //     // var list = [];
    //     that.data.hcchooseList.push(data);
    //     wx.setStorageSync('hcchooseList', that.data.hcchooseList);
    //     that.setData(that.data)
    //   }

    // })
    requestDataparams('/api/checkContents', JSON.stringify(data), 'PUT').then(res => {
      if (res.data.code == 0) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.loadData();

        prevPage.setData(prevPage.data);
        wx.navigateBack();
        // wx.redirectTo({
        //   url: '../patrol_record/patrol_record',
        // })

      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        });
      }
    }).catch(err => {

    });

  },
  submithc() {
    // var list = wx.getStorageSync('chooseList');
  },
  loadData() {
    var that = this;
    let data = {
      "checkTaskId": that.data.checkTaskId,
      "checkContentForeignId": that.data.foreignId,
      'checkStandardForeignId': that.data.deviceClassificationId,
      'checkStandardForeignType': 2
    };
    // wx.request({
    //   url: applet_park_api_url + '/api/checkStandards',
    //   method: 'get',
    //   data: {
    //     "checkTaskId": that.data.checkTaskId,
    //     "checkContentForeignId": that.data.foreignId,
    //     'checkStandardForeignId': that.data.deviceClassificationId,
    //     'checkStandardForeignType': 2
    //   },
    //   success: function(res) {
    //     )
    //     if (res.data.code == 0) {
    //       var lists = res.data.data;
    //       for (let i in lists) {
    //         lists[i].open = true;
    //       }
    //       that.setData({
    //         lists: lists
    //       })
    //     } else {
    //       wx.showToast({
    //         title: res.data.msg,
    //         icon: 'none'
    //       });
    //     }
    //   },
    //   fail: function(res) {
    //     )
    //   }

    // })

    requestDataparams('/api/checkStandards', data, 'GET').then(res => {
      if (res.data.code == 0) {
        var lists = res.data.data;
        for (let i in lists) {
          lists[i].open = true;
        }
        that.setData({
          lists: lists
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        });
      }
    }).catch(err => {

    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var hcchooseList = wx.getStorageSync('hcchooseList');
    this.data.hcchooseList = hcchooseList;
    this.setData(this.data);
  },
  hcsubmit() {

  },
  confirmnode() {
    var that = this;
    let url = '/api/checkTaskNodes/' + that.data.checktasknodeid + '/confirm';
    // wx.request({
    //   url: applet_park_api_url + '/api/checkTaskNodes/' + that.data.checktasknodeid + '/confirm',
    //   method: 'PUT',
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded'
    //   },
    //   success: function(res) {
    //     )
    //     if (res.data.code == 0) {
    //       wx.showToast({
    //         title: '操作成功',
    //       })
    //       var pages = getCurrentPages();
    //       var prevPage = pages[pages.length - 2]; //上一个页面
    //       prevPage.loadData();

    //       prevPage.setData(prevPage.data);
    //       wx.navigateBack({})
    //     } else {
    //       wx.showToast({
    //         title: res.data.msg,
    //         icon: 'none'
    //       });
    //     }
    //   },
    //   fail: function(res) {
    //     )
    //   }

    // })

    requestDataparams(url, {}, 'PUT').then(res => {
      if (res.data.code == 0) {
        wx.showToast({
          title: '操作成功',
        })
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.loadData();

        prevPage.setData(prevPage.data);
        wx.navigateBack({})
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        });
      }
    }).catch(err => {

    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})